package cn.tedu.department.controller;

import cn.tedu.department.pojo.dto.DepartmentDTO;
import cn.tedu.department.pojo.vo.DepartmentVO;
import cn.tedu.department.service.DepartmentService;
import cn.tedu.department.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/v1/department/")
@Api(tags = "部门")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("列表")
    @GetMapping("list")
    public JsonResult list(DepartmentDTO departmentDTO) {
        //外部调用时,只允许查询已启用的部门
        departmentDTO.setStatus("1");
        List<DepartmentVO> list = departmentService.selectAll(departmentDTO);
        return JsonResult.ok(list);
    }

    @ApiOperation("树形结构")
    @GetMapping("getTree")
    public JsonResult getTree(DepartmentDTO departmentDTO) {
        Map<String, Object> data = departmentService.getTree(departmentDTO);
        return JsonResult.ok(data);
    }

    @ApiOperation("更新")
    @PostMapping("update")
    public JsonResult update(@RequestBody DepartmentDTO departmentDTO) {
        log.debug("获取到参数:{}", departmentDTO);
        departmentService.update(departmentDTO);
        return JsonResult.ok();
    }

    @ApiOperation("修改状态")
    @GetMapping("changeStatus")
    public JsonResult changeStatus(DepartmentDTO departmentDTO) {
        departmentService.changeStatus(departmentDTO);
        return JsonResult.ok(200, "操作成功");
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public JsonResult delete(DepartmentDTO departmentDTO) {
        departmentService.delete(departmentDTO);
        return JsonResult.ok();
    }
}
